<template>
  <a-modal
    class="formModal"
    :title="$t('form.updateName.title')"
    v-model:open="modalVisible"
    @cancel="close"
    :footer="null"
  >
    <a-form ref="formRef" :model="formState">
      <a-form-item
        :label="$t('form.updateName.form.item_1.label')"
        name="value"
        :rules="[
          {
            required: true,
            message: $t('form.updateName.form.item_1.rules.error_1'),
            trigger: 'blur',
          },
        ]"
      >
        <a-input
          :maxlength="30"
          v-model:value.trim="formState.value"
          :placeholder="$t('form.updateName.form.item_1.rules.error_1')"
          allowClear
        ></a-input>
      </a-form-item>
      <a-form-item
        :label="$t('form.updateName.form.item_2.label')"
        name="value2"
        :rules="[
          {
            required: true,
            message: $t('form.updateName.form.item_2.rules.error_1'),
            trigger: 'blur',
          },
        ]"
      >
        <a-input
          :maxlength="30"
          v-model:value.trim="formState.value2"
          :placeholder="$t('form.updateName.form.item_2.rules.error_1')"
          allowClear
        ></a-input>
      </a-form-item>
    </a-form>
    <div class="footer-btns">
      <a-button @click="close">{{ $t("btnText.cancel") }}</a-button>
      <a-button type="primary" :loading="isSubmitting" @click="submit">{{
        $t("btnText.save")
      }}</a-button>
    </div>
  </a-modal>
</template>

<script setup lang="ts">
import { getCurrentInstance, ref } from "vue";
import { postUpdateMemberOne } from "../../../api/member";
import { useMemberStore } from "../../../store";
import { message } from "ant-design-vue";

const $t = getCurrentInstance()?.appContext.config.globalProperties.$t;

const memberStore = useMemberStore();
const modalVisible = ref(false);
const close = () => {
  modalVisible.value = false;
  formRef.value.resetFields();
  formState.value.value = "";
  formState.value.value2 = "";
};

const show = () => {
  formState.value.value = memberStore.profile?.last_name! || "";
  formState.value.value2 = memberStore.profile?.first_name! || "";
  modalVisible.value = true;
};

defineExpose({
  close,
  show,
});

// ===============form state================
const formRef = ref();

const formState = ref({
  value: "",
  value2: "",
});

const isSubmitting = ref(false);
const submit = () => {
  formRef.value.validate().then(async () => {
    let data = {
      field: "last_name",
      value: formState.value.value,
    };
    let data2 = {
      field: "first_name",
      value: formState.value.value2,
    };
    isSubmitting.value = true;
    let res = await postUpdateMemberOne(data);
    let res2 = await postUpdateMemberOne(data2);
    isSubmitting.value = false;
    if (res.code === 200 && res2.code === 200) {
      memberStore.profile.last_name = formState.value.value;
      memberStore.profile.first_name = formState.value.value2;
      message.success($t("message.changed_success"));
      close();
    } else {
      message.error($t('message.error.update'));
    }
  });
};
</script>
<style scoped lang="less"></style>
